package behavioral_pattern.chain_pattern;

import java.util.ArrayList;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/12/04 16:53
 */


public class PurchaseRequest {
    private double amount;  //采购金额
    private int number; //采购编号
    private String purpose; //采购目的

    public PurchaseRequest(double amount, int number, String purpose) {
        this.amount = amount;
        this.number = number;
        this.purpose = purpose;
    }

    public static void main(String[] args) {

        PurchaseRequest request = new PurchaseRequest(210000, 123, "caonima");
        //初始化
        Approver director = new Director("director");
        Approver vicePresident = new VicePresident("vicePresident");
        Approver president = new President("president");
        Approver congress = new Congress("congress");
        //设置职责链
        director.setNext(vicePresident);
        vicePresident.setNext(president);
        president.setNext(congress);
        //执行任务
        director.processRequest(request);

    }
    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getPurpose() {
        return purpose;
    }

    public void setPurpose(String purpose) {
        this.purpose = purpose;
    }
}
//审批者
abstract class Approver{
    protected Approver next;
    protected String name;

    public Approver(String name) {
        this.name = name;
    }

    public void setNext(Approver next) {
        this.next = next;
    }

    abstract void processRequest(PurchaseRequest request);

}

class Director extends Approver {
    public Director(String name) {
        super(name);
    }

    @Override
    void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 50000) {
            System.out.println("Director 审批通过，结束！");
        }else{
            next.processRequest(request);
        }
    }
}
class VicePresident extends Approver {
    public VicePresident(String name) {
        super(name);
    }

    @Override
    void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 100000) {
            System.out.println("VicePresident 审批通过，结束！");
        }else{
            next.processRequest(request);
        }
    }
}
class President extends Approver {
    public President(String name) {
        super(name);
    }

    @Override
    void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 200000) {
            System.out.println("President 审批通过，结束！");
        }else{
            next.processRequest(request);
        }
    }
}
class Congress extends Approver {
    public Congress(String name) {
        super(name);
    }

    @Override
    void processRequest(PurchaseRequest request) {
        if (request.getAmount() < 400000) {
            System.out.println("Congress 审批通过，结束！");
        }else{
            System.out.println("end!");
        }
    }
}